<template>
    <div>
        <div class="mc-common-title">
            <div class="mcct-left">
                <span></span>
                <span>常用地址</span>
            </div>
            <button
                    class="mcct-right"
                    @click="openaddaddress()"
            >新增地址
            </button>
        </div>
        <el-table
                :data="addressData"
                style="width: 100%"
                :header-cell-style="{ background: '#F2F2F2', color: '#333333' }"
        >
            <el-table-column

                    type="index"
                    label="序号"
            >
            </el-table-column>
<!--            <el-table-column-->
<!--                    prop="storeCode"-->
<!--                    label="门店编码"-->
<!--            >-->
<!--            </el-table-column>-->

            <el-table-column
                    prop="person"
                    label="收货人"
            >
            </el-table-column>
            <el-table-column
                    prop="phone"
                    label="收货电话"
            >
            </el-table-column>
            <el-table-column
                    prop="address"
                    label="地址"
            >
            </el-table-column>
            <el-table-column
                    prop="label"
                    label="标签"
            >
            </el-table-column>

            <el-table-column
                    fixed="right"
                    label="操作"
                    width="100"
            >
                <template slot-scope="props">

                    <el-button
                            @click="opendeleteaddress(props.row.id)"
                            type="text"
                            size="small"
                            :style="{color: '#C29C0F'}"
                    >删除
                    </el-button>

                    <el-button
                            @click="openupdatestoreaddress(props.row)"
                            type="text"
                            size="small"
                            :style="{color: '#C29C0F'}"
                    >编辑
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 添加地址的窗口-->
        <el-dialog
                title="添加常用地址"
                :visible.sync="addaddressVisible"
                width="480px"
                :close-on-click-modal="false"
        >
            <span style="color: #ee0a0a;margin-right: 2px">*</span>常用地址
            <el-input
                    v-model="storeaddress"
                    placeholder="请填写地址"
                    :style="{marginBottom: '20px', width: '76%', marginRight: '25px',marginLeft:'9px'}"
            ></el-input>
            <br/>

            <span style="color: #ee0a0a;margin-right: 2px">*</span>收货人
            <el-input
                    v-model="person"
                    placeholder="请填写收货人"
                    :style="{marginBottom: '20px', width: '46%', marginRight: '25px',marginLeft:'22px'}"
            ></el-input>
            <br/>

            <span style="color: #ee0a0a;margin-right: 2px">*</span>收货电话
            <el-input
                    v-model="phone"
                    placeholder="请填写收货电话"
                    :style="{marginBottom: '20px', width: '46%', marginRight: '25px',marginLeft:'9px'}"
            ></el-input>
            <br/>
            <span style="margin-left: 5px">标 签</span>
            <el-input
                    v-model="label"
                    placeholder="请填写标签(例如:公司/家)"
                    :style="{marginBottom: '20px', width: '46%', marginRight: '25px',marginLeft:'36px'}"
            ></el-input>
            <br/>
            <el-button
                    type="primary"
                    @click="addstoreaddress"
            >确 定
            </el-button>
        </el-dialog>

        <!-- 添加地址的窗口-->
        <el-dialog
                title="编辑地址"
                :visible.sync="updateaddressVisible"
                width="480px"
                :close-on-click-modal="false"
        >
            <span style="color: #ee0a0a;margin-right: 2px">*</span>常用地址
            <el-input
                    v-model="storeaddress"
                    placeholder="请填写地址"
                    :style="{marginBottom: '20px', width: '76%', marginRight: '25px',marginLeft:'9px'}"
            ></el-input>
            <br/>

            <span style="color: #ee0a0a;margin-right: 2px">*</span>收货人
            <el-input
                    v-model="person"
                    placeholder="请填写收货人"
                    :style="{marginBottom: '20px', width: '46%', marginRight: '25px',marginLeft:'22px'}"
            ></el-input>
            <br/>

            <span style="color: #ee0a0a;margin-right: 2px">*</span>收货电话
            <el-input
                    v-model="phone"
                    placeholder="请填写收货电话"
                    :style="{marginBottom: '20px', width: '46%', marginRight: '25px',marginLeft:'9px'}"
            ></el-input>
            <br/>
            <span style="margin-left: 5px">标 签</span>
            <el-input
                    v-model="label"
                    placeholder="请填写标签(例如:公司/家)"
                    :style="{marginBottom: '20px', width: '46%', marginRight: '25px',marginLeft:'36px'}"
            ></el-input>
            <br/>
            <el-button
                    type="primary"
                    @click="updatestoreaddress"
            >确 定
            </el-button>
        </el-dialog>

        <!--取消订单的弹框-->
        <el-dialog
                title="提示"
                :visible.sync="dialogVisible4"
                width="30%"
        >
            <span>确定删除门店地址吗？</span>
            <span slot="footer" class="dialog-footer">
				   <el-button @click="dialogVisible4 = false">取 消</el-button>
				   <el-button type="primary"
                              @click="dialogVisible4 = false,deletestoreaddress(deladdressId)">确 定</el-button>
				 </span>
        </el-dialog>
    </div>
</template>

<script>
    import {getAction, postAction} from '@/api/manage'

    export default {
        data() {
            return {
                deladdressId: '',
                label: '',
                addressid: '',
                person: '',
                phone: '',
                storeaddress: '',
                addaddressVisible: false,
                addressVisible: false,
                updateaddressVisible: false,
                id: '',
                index: '',
                dialogVisible3: false,
                dialogVisible4: false,
                token: localStorage.getItem("token"),
                storeId:'',//门店id
                storeCode: '',//门店编码
                companyId: "",//公司id
                companyCode:'',//公司编码
                dialogVisible2: false,
                dialogVisible: false,
                currentIndex: 2,
                ruleForm: {
                    name: '',
                    num: "",
                    phone: "",
                    address: "",
                    aboutPerson: "",
                    username: "",
                    dianzhangname: "",
                    dianzhangworknumber: "",
                    dianzhangphone: ""
                },
                url: {
                    list: '/company/querystoreBycompanyId',
                    add: '/company/addstore',
                    del: '/company/deletestore',
                    detail: '/company/getstorebyid',
                    update: '/company/updatestore',
                    queryCompanyId: '/company/queryCompanyId',
                    queryCompanyCode: "/company/queryCompanyCode",
                    Queryaddress: '/company/getaddressbystoreId',
                    addstoreaddress: '/company/addstoreaddress',
                    deletestoreaddress: '/company/deletestoreaddress',
                    updatestoreaddress: '/company/updatestoreaddress'
                },
                tableData: [],
                addressData: [],
            };
        },
        created() {
            //取到公司id
            getAction(domain.publicUrl + this.url.queryCompanyId, {'token': this.token}).then(res => {
                this.companyId = res.data.data
                this.companyCode = res.data.companyCode;
            })
            this.getDate();//加载列表数据
        },
        methods: {
            openupdatestoreaddress(record) {
                this.updateaddressVisible = true;
                this.storeaddress = record.address;
                this.phone = record.phone;
                this.person = record.person;
                this.addressid = record.id;
                this.label = record.label;
            },
            opendeleteaddress(id) {
                this.deladdressId = id;
                this.dialogVisible4 = true;
            },
            deletestoreaddress(id) {
                getAction(domain.publicUrl + this.url.deletestoreaddress, {'id': id}).then(res => {
                    if (res.data.code == '0') {
                        this.$message.success("操作成功！")
                        this.getDate();
                    }
                })
            },
            updatestoreaddress() {
                if (this.storeaddress == '' || this.storeaddress == null) {
                    this.$notify.warning({
                        title: "提示",
                        message: "请输入常用地址",
                        duration: 1500,
                    });
                    return;
                }
                if (this.person == '' || this.person == null) {
                    this.$notify.warning({
                        title: "提示",
                        message: "请输入收货人",
                        duration: 1500,
                    });
                    return;
                }
                if (this.phone == '' || this.phone == null) {
                    this.$notify.warning({
                        title: "提示",
                        message: "请输入联系电话",
                        duration: 1500,
                    });
                    return;
                }
                let params = {
                    id: this.addressid,
                    address: this.storeaddress,
                    label: this.label,
                    person: this.person,
                    phone: this.phone
                };
                postAction(domain.publicUrl + this.url.updatestoreaddress, params).then(res => {
                    if (res.data.code == '0') {
                        this.$message.success("修改成功！")
                        this.updateaddressVisible = false;
                        this.getDate()
                    } else {
                        this.$message.error(res.data.data)
                    }
                })
            },

            addstoreaddress() {
                let params = {
                    id:this.storeId,
                    storeCode: this.storeCode,
                    companyId: this.companyId,
                    companyCode: this.companyCode,
                    address: this.storeaddress,
                    person: this.person,
                    phone: this.phone,
                    label: this.label
                };
                if (this.storeaddress == '' || this.storeaddress == null) {
                    this.$notify.warning({
                        title: "提示",
                        message: "请输入常用地址",
                        duration: 1500,
                    });
                    return;
                }
                if (this.person == '' || this.person == null) {
                    this.$notify.warning({
                        title: "提示",
                        message: "请输入收货人",
                        duration: 1500,
                    });
                    return;
                }
                if (this.phone == '' || this.phone == null) {
                    this.$notify.warning({
                        title: "提示",
                        message: "请输入联系电话",
                        duration: 1500,
                    });
                    return;
                }
                postAction(domain.publicUrl + this.url.addstoreaddress, params).then(res => {
                    if (res.data.code == '0') {
                        this.$message.success("新增成功！")
                        this.addaddressVisible = false;
                        this.getDate()
                    } else {
                        this.$message.error(res.data.data)
                    }
                })
            },

            //打开添加地址窗口
            openaddaddress() {
                this.storeaddress = '';
                this.person = '';
                this.phone = '';
                this.label = '';
                this.addaddressVisible = true;
            },

            // 打开取消顶订单的弹框
            openquxiao(id, index) {
                this.dialogVisible3 = true
                this.id = id
                this.index = index
            },
            //清除方法
            qingchu() {
                this.ruleForm.name = "";
                this.ruleForm.num = "";
                this.ruleForm.phone = "";
                this.ruleForm.address = "";
                this.ruleForm.aboutPerson = "";
                this.ruleForm.username = "";
                this.ruleForm.dianzhangname = "";
                this.ruleForm.dianzhangworknumber = "";
                this.ruleForm.dianzhangphone = "";
            },
            // 加载数据列表
            getDate() {
                getAction(domain.publicUrl + this.url.Queryaddress, {
                    'token': localStorage.getItem("token")
                }).then(res => {
                    this.addressData = res.data.data.records
                })
            },
        },
    };
</script>

<style lang="scss" scoped>
    .mc-common-title {
        display: flex;
        justify-content: space-between;
        margin-top: 40px;
        margin-bottom: 20px;

        .mcct-left {
            display: flex;
            align-items: center;

            span:nth-child(1) {
                display: block;
                width: 5px;
                height: 25px;
                background: #4b4b4b;
            }

            span:nth-child(2) {
                font-size: 16px;
                font-weight: 500;
                color: #4e4e4e;
                margin-left: 15px;
            }
        }

        .mcct-right {
            width: 84px;
            height: 38px;
            background: #333333;
            border-radius: 3px;
            color: #f9d532;
            font-size: 14px;
            cursor: pointer;
        }
    }
</style>
